package com.nb.controller;

import com.mybatisflex.core.paginate.Page;
import com.nb.annotation.LogAsync;
import com.nb.dto.*;
import com.nb.entity.User;
import com.nb.result.Result;
import com.nb.service.UserService;
import com.nb.vo.*;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    //登录
    @PostMapping("/login")
    public Result login(@RequestBody LoginDTO loginDTO) {
        LoginVO loginVO = userService.login(loginDTO);
        return Result.success(loginVO);
    }

    // 新增注册接口
    @LogAsync("用户注册") // 记录注册操作日志
    @PostMapping("/register/public")
    public Result register(@Validated @RequestBody RegisterDTO registerDTO) {
        // 调用服务层注册方法
        RegisterVO registerVO = userService.register(registerDTO);
        return Result.success(registerVO);
    }

    // 律师注册接口
    @LogAsync("律师注册")
    @PostMapping("/register/lawyer")
    public Result registerLawyer(@Validated @RequestBody LawyerRegisterDTO registerDTO) {
        RegisterVO registerVO = userService.registerLawyer(registerDTO);
        return Result.success(registerVO);
    }
    @LogAsync(value = "查询用户列表", recordParams = true)  // 保留日志注解
    @GetMapping("/list")  // URL路径保持不变
    public Result getUserList() {  // 移除分页查询参数queryDTO
        // 调用查询所有用户的服务方法，替换分页查询
        List<User> userList = userService.list();
        return Result.success(userList);
    }


    @PostMapping("/create")
    public Result create(@RequestBody @Valid CreateUserDTO createUserDTO) {
        Long userId = userService.createUser(createUserDTO);
        return Result.success(userId);
    }

    @PostMapping("/update")
    public Result update(@RequestBody UpdateUserDTO updateUserDTO) {
        userService.updateUser(updateUserDTO);

        return Result.success();
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Long id) {

        userService.deleteUser(id);
        return Result.success();
    }

    //根据id查询用户
    @GetMapping("/{id}")
    public Result getUserById(@PathVariable Long id) {
        User user = userService.getUserById(id);
        return Result.success(user);
    }

    //根据用户名查询用户
    @GetMapping("/name/{username}")
    public Result getUserByUsername(@PathVariable String username) {
        User user = userService.getByUsername(username);
        return Result.success(user);
    }

    //分页查询用户
    @GetMapping("/page")
    public Result getUserPage(@Valid UserQueryDTO queryDTO) {
        Page<UserVO> page = userService.getUserPage(queryDTO);
        return Result.success(page);
    }

    //    @LogAsync(value = "重置用户密码", recordParams = true)
    @PostMapping("/resetPassword")
    public Result resetPassword(@RequestBody @Valid ResetPasswordDTO resetPasswordDTO) {
        userService.resetPassword(resetPasswordDTO);
        return Result.success();
    }

    //    @LogAsync(value = "查询用户详情", recordParams = true)
    @GetMapping("/detail/{id}")
    public Result getUserDetail(@PathVariable Long id) {
        UserVO userVO = userService.getUserDetail(id);
        if (userVO == null) {
            return Result.error("用户不存在或已被删除");
        }
        return Result.success(userVO);
    }

    @PostMapping("/status")
    public Result updateStatus(@RequestBody StatusDTO statusDTO) {
        userService.updateStatus(statusDTO);
        return Result.success();
    }
}
